package com.heyu.cgsd.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.heyu.cgsd.R;
import com.heyu.cgsd.model.bean.SettingBean;
import com.heyu.cgsd.ui.activity.AboutActivity;
import com.heyu.cgsd.ui.activity.UserServiceAgreementActivity;
import com.heyu.cgsd.util.CleanDataUtils;
import com.heyu.cgsd.util.SystemUtil;
import com.heyu.cgsd.util.ToastUtil;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

public class SettingAdapter extends BaseQuickAdapter<SettingBean, BaseViewHolder> {

    public SettingAdapter() {
        super(R.layout.item_setting, null);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, SettingBean item) {
        TextView tvItem = helper.getView(R.id.tv_item);
        TextView tvContent = helper.getView(R.id.tv_content);
        ImageView imgItem = helper.getView(R.id.img_item);

        int position = helper.getLayoutPosition();
        Context context = helper.itemView.getContext();
        Glide.with(context).load(item.getItemIcon()).into(imgItem);

        tvItem.setText(item.getItemName());
        tvContent.setVisibility(View.VISIBLE);
        if(position == 0){
            tvItem.setText(item.getItemName() + "\u0000");
        }
        if (position == 1) {//清理缓存
            try {
                tvContent.setText(CleanDataUtils.getTotalCacheSize(context));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (position == 4) {//版本信息
            tvContent.setText("V" + SystemUtil.getVersionName());
        } else {
            tvContent.setVisibility(View.GONE);
        }

        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (position){
                    case 1:
                        new QMUIDialog.MessageDialogBuilder(context)
                                .setMessage("确认清理吗?")
                                .setCancelable(false)//禁止按下返回false
                                .setCanceledOnTouchOutside(false)//禁止触摸false
                                .addAction("取消", new QMUIDialogAction.ActionListener() {
                                    @Override
                                    public void onClick(QMUIDialog dialog, int index) {
                                        dialog.dismiss();
                                    }
                                }).addAction("确认", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                                CleanDataUtils.clearAllCache(context);//清理缓存
                                notifyItemChanged(position);
                            }
                        }).show();
                        break;
                    case 2:
                        UserServiceAgreementActivity.startAct((Activity) context, 1);
                        break;
                    case 3:
                        UserServiceAgreementActivity.startAct((Activity) context, 0);
                        break;
                    case 4:
                        ToastUtil.showShortToast(context, "当前已是最新版本");
                        break;
                    case 5:
                        startAct((Activity) context, new AboutActivity());
                        break;
                }
            }
        });
    }

    /**
     * Activity跳转
     *
     * @param startAct
     * @param endAct
     * @return
     */
    public void startAct(Activity startAct, Activity endAct) {
        Intent intent = new Intent(startAct, endAct.getClass());
        startAct.startActivity(intent);
    }
}
